package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class RadioLinearLayoutAdapter extends RecyclerView.Adapter<RadioLinearLayoutAdapter.RadioViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<Rank> radioList;

    private mItemClickListener itemClickListener;

    private int width;

    public RadioLinearLayoutAdapter(Context context, List<Rank> radioList) {
        this.context = context;
        this.radioList = radioList;

        inflater = LayoutInflater.from(context);

        width = context.getResources().getDisplayMetrics().widthPixels;
    }


    /**
     * 设置监听事件
     * @param listener
     */
    public void setOnItemClickListener(mItemClickListener listener)
    {
        this.itemClickListener = listener;
    }

    @Override
    public RadioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = inflater.inflate(R.layout.radio_item,null);

        return new RadioViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RadioViewHolder holder, final int position) {

        Rank radio = radioList.get(position);

        LogUtil.w("radio.getImage_url() = " + radio.getImage_url());

        ViewGroup.LayoutParams layoutParams = holder.imageView.getLayoutParams();
        layoutParams.width = width;
        holder.imageView.setLayoutParams(layoutParams);

        ImageLoader.getInstance().displayImage(radio.getImage_url(),holder.imageView, ImageUtil.getDefaultOptions());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {

        return radioList == null ? 0 : radioList.size();
    }

    public class RadioViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;

        public RadioViewHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.radio_item_iv);
        }
    }

    public interface mItemClickListener
    {
        void onItemClick(int position);
    }
}
